package com.xone.collaboration.service.impl;

import com.xone.collaboration.dao.dataobject.*;
import com.xone.collaboration.dao.mapper.GroupMapper;
import com.xone.collaboration.dao.mapper.LayerMapper;
import com.xone.collaboration.dao.mapper.SketchpadMapper;
import com.xone.collaboration.service.SketchpadService;
import com.xone.collaboration.service.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class SketchpadServiceImpl implements SketchpadService {

    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private SketchpadMapper sketchpadMapper;
    @Autowired
    private LayerMapper layerMapper;

    @Override
    public int createGroup(CreateGroupVO groupVO) {
        Group group = new Group();
        BeanUtils.copyProperties(groupVO, group);
        String groupId = UUID.randomUUID().toString();
        group.setGmtCreate(new Date());
        group.setGmtModified(new Date());
        group.setGroupId(UUID.randomUUID().toString());

        Sketchpad sketchpad = new Sketchpad();
        sketchpad.setGmtCreate(new Date());
        sketchpad.setGmtModified(new Date());
        sketchpad.setSketchpadId(UUID.randomUUID().toString());
        sketchpad.setGroupId(groupId);

        sketchpadMapper.insert(sketchpad);
        return groupMapper.insert(group);
    }

    @Override
    public int createSketchpad(CreateSketchpadVO sketchpadVO) {
        Sketchpad sketchpad = new Sketchpad();
        BeanUtils.copyProperties(sketchpadVO, sketchpad);
        sketchpad.setGmtCreate(new Date());
        sketchpad.setGmtModified(new Date());
        sketchpad.setSketchpadId(UUID.randomUUID().toString());
        return sketchpadMapper.insert(sketchpad);
    }

    @Override
    public int deleteSketchpad(String sketchpadId) {
        SketchpadExample example = new SketchpadExample();
        example.createCriteria().andSketchpadIdEqualTo(sketchpadId);
        return sketchpadMapper.deleteByExample(example);
    }

    @Override
    public QueryGroupVO queryGroup(String groupId) {

        GroupExample example = new GroupExample();
        example.createCriteria().andGroupIdEqualTo(groupId);
        List<Group> groups = groupMapper.selectByExample(example);
        QueryGroupVO queryGroupVO = new QueryGroupVO();
        if (groups != null && groups.size() > 0) {
            Group group = groups.get(0);
            BeanUtils.copyProperties(group, queryGroupVO);
            SketchpadExample sketchpadExample = new SketchpadExample();
            sketchpadExample.createCriteria().andGroupIdEqualTo(group.getGroupId());
            List<Sketchpad> sketchpads = sketchpadMapper.selectByExample(sketchpadExample);
            List<String> skIds = sketchpads != null && sketchpads.size() > 0
                    ? sketchpads.stream().map(sk -> sk.getSketchpadId()).collect(Collectors.toList())
                    : null;
            queryGroupVO.setSketchpadIds(skIds);
        } else {
            return null;
        }
        return queryGroupVO;
    }

    @Override
    public QuerySketchpadVO querySketchpad(String sketchpadId) {
        SketchpadExample sketchpadExample = new SketchpadExample();
        sketchpadExample.createCriteria().andSketchpadIdEqualTo(sketchpadId);
        List<Sketchpad> sketchpads = sketchpadMapper.selectByExample(sketchpadExample);
        QuerySketchpadVO querySketchpadVO = new QuerySketchpadVO();
        if (sketchpads != null && sketchpads.size()>0) {
            Sketchpad sketchpad = sketchpads.get(0);
            BeanUtils.copyProperties(sketchpad, querySketchpadVO);
            LayerExample layerExample = new LayerExample();
            layerExample.createCriteria().andSketchpadIdEqualTo(sketchpadId);
            List<Layer> layers = layerMapper.selectByExampleWithBLOBs(layerExample);
            if (layers != null && layers.size()>0) {
                List<LayerVO> layerVOList = layers.stream().map(layer -> {
                    LayerVO layerVO = new LayerVO();
                    BeanUtils.copyProperties(layer, layerVO);
                    return layerVO;
                }).collect(Collectors.toList());
                querySketchpadVO.setLayers(layerVOList);
            }
        } else {
            return null;
        }
        return querySketchpadVO;
    }
}
